﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB;
using ScheduleMaker.DB.CoreDAO;


namespace ScheduleMaker
{
    public partial class smAdmGroupEdit : Form
    {
        //smAdminForm parentForm;
        DB.Domain.Core.Group group;
        IList<DB.Domain.Core.Subject> subjects;

        public smAdmGroupEdit(DB.Domain.Core.Group _group)
        {
            InitializeComponent();
            //this.parentForm = _parentForm;
            this.group = _group;

            lblGroupName.Text = this.group.Name;

            this.getSubjectList();
            this.getGroupSubjectList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int selectedIndex = lbSubjects.SelectedIndex;
            int numberHours = 0;

            try
            {
                DB.Domain.Core.Subject subject = subjects.ElementAt(selectedIndex);
                SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                if (!group.SubjectsCount.Keys.Contains(subject))
                {
                    GroupDAO groupDAO = DAOFactory.getGroupDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                    using (smAdminSubjectAddHour subjectAddHourForm = new smAdminSubjectAddHour(1))
                    {
                        subjectAddHourForm.ShowDialog();
                        numberHours = subjectAddHourForm.numberHours;
                    }

                    // Dodanie przedmiotu wraz z ilością godzin
                    group.SubjectsCount.Add(subject, numberHours);

                    //group.Subjects.Add(subject);

                    groupDAO.saveOrUpdate(group);
                }
                else
                {
                    MessageBox.Show("Przedmiot już dodano!", "Błąd " + selectedIndex);
                }
                //this.getUserList();
                this.getGroupSubjectList();
            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }
        }

        // Pobieranie - przedmiotów
        private void getSubjectList()
        {
            lbSubjects.Items.Clear();
            SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);
            subjects = subjectDAO.getAll();
            foreach (var elem in subjects)
            {
                lbSubjects.Items.Add(elem.Id + ": " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }
        }

        private void getGroupSubjectList()
        {
            lbGroupSubjects.Items.Clear();
            //group.
            
            foreach (var elem in group.SubjectsCount.Keys) // group.Subjects)
            {
                lbGroupSubjects.Items.Add(elem.Id + ": " + elem.Name + " - " + group.SubjectsCount[elem]);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // coś tam xD - gdy usuwamy
            int selectedIndex = lbGroupSubjects.SelectedIndex;
            try
            {
                if (MessageBox.Show("Czy na pewno chcesz usunąć ten przedmiot?", "Potwierdź usunięcie", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //DB.Domain.Subject subject = group.Subjects.ElementAt(selectedIndex);
                    DB.Domain.Core.Subject subject = group.SubjectsCount.Keys.ElementAt(selectedIndex);

                    GroupDAO groupDAO = DAOFactory.getGroupDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                    //group.SubjectsCount.Keys.Remove(subject);
                    //group.Subjects.Remove(subject);
                    group.SubjectsCount.Remove(subject);
                    groupDAO.saveOrUpdate(group);

                    // odświeżenie listy
                    this.getGroupSubjectList();
                }
            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }
        }

        private void smAdmGroupEdit_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // coś tam xD - gdy edutujemy
            int selectedIndex = lbGroupSubjects.SelectedIndex;
            // Liczba godzin
            int numberHours = 0;
            try
            {
                //DB.Domain.Subject subject = group.Subjects.ElementAt(selectedIndex);
                DB.Domain.Core.Subject subject = group.SubjectsCount.Keys.ElementAt(selectedIndex);
                GroupDAO groupDAO = DAOFactory.getGroupDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                //group.Subjects.Remove(subject);

                int hours = group.SubjectsCount.Values.ElementAt(selectedIndex);
                using (smAdminSubjectAddHour subjectAddHourForm = new smAdminSubjectAddHour( hours))
                {
                    subjectAddHourForm.ShowDialog();
                    numberHours = subjectAddHourForm.numberHours;

                    /*
                    // sprawdzanie czy przedmiot już jest dodany do grupy
                    if (group.SubjectsCount.ContainsKey( subject))
                    {
                        group.SubjectsCount.Remove(subject);
                    }
                     * */
                    if (group.SubjectsCount.Keys.Contains(subject))
                    {
                        group.SubjectsCount.Remove(subject);
                    }
                    //group.SubjectsCount.
                }

                group.SubjectsCount.Add(subject, numberHours);
                groupDAO.saveOrUpdate(group);

                // odświeżenie listy
                this.getGroupSubjectList();
            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }
        }
    }
}
